import { Component, OnInit, Output, EventEmitter, Input, OnDestroy } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { ToolsService } from 'app/shared/service';
import { NzMessageService, NzModalService } from 'ng-zorro-antd';
import { ScheduledService } from '../scheduled.service';
import { Router } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';
import { differenceInCalendarDays } from 'date-fns';
import { BullySubjectService } from 'app/shared/service';
import { MESSAGE_CHANNEL, SELECT_OPTIONS } from 'app/app.constants';
import { Subscription } from 'rxjs';
import { PublicService } from 'app/public/public.service';
// import { IncidentService } from '../../incident-record/incident.service';
import * as moment from 'moment';
import { SystemService } from 'app/service/system.service';
import { AuthService } from 'app/auth/auth.service';

@Component({
    selector: 'itmp-ir-info-details',
    templateUrl: './ir-info-details.component.html',
    styleUrls: ['./ir-info-details.component.css']
})
export class IrInfoDetailsComponent implements OnInit, OnDestroy {
    @Output() cancelEdit = new EventEmitter<any>();
    @Output() isIrCreated = new EventEmitter<any>();
    @Input() isCreate = new EventEmitter<any>();
    @Output() clickEdit = new EventEmitter<any>();
    @Input() accessButton;

    validateForm: FormGroup;
    isEditable = false;
    ifMoreInfoShow = false;
    isVerify = false;
    isFinishShow = false;
    isBeginBow = false;
    radioValue = null;
    createAccess = {};
    listOfData = [];
    listOfData2 = [];
    isUpdate: any = false;
    subscription: Subscription = null;
    inGet = {
        startCoor: false,
        endCoor: false
    };
    LANE_BLOCKAGES = ['arrow-up.png', 'cross.png'];
    listOfOption: Array<{ label: string; value: string }> = [
        { label: 'arrow-up.png', value: 'arrow-up.png' },
        { label: 'cross.png', value: 'cross.png' }
    ];
    laneBlockagesList = [];
    zoneIdList = [];
    isForce = false; // 传递的参数isForce
    isForceVisible = false; // 强制执行弹出框
    isForceCancelVisible = false; // 强制取消计划弹框
    isCancelForce = false;
    isForceBeginNowVisible = false; // 强制beginNow
    renderPartner: any = [
        {
            name: 'ID',
            key: null,
            value: 'id',
            isChecked: true
        },
        {
            name: 'Type',
            key: null,
            value: 'type',
            isChecked: true
        },
        {
            name: 'VehicleNO',
            key: null,
            value: 'VehicleNO',
            isChecked: true
        },
        {
            name: 'OfficerName',
            key: null,
            value: 'OfficerName',
            isChecked: true
        },
        {
            name: 'OfficerContact',
            key: null,
            value: 'OfficerContact',
            isChecked: true
        },
        {
            name: 'Departure',
            key: null,
            value: 'Departure',
            isChecked: true
        },
        {
            name: 'Arrival',
            key: null,
            value: 'Arrival',
            isChecked: true
        },
        {
            name: 'Notify',
            key: null,
            value: 'Notify',
            isChecked: true
        }
    ];
    incidentType: Array<any>;
    verificationStatus: Array<any>;
    locationTypeList: Array<any>;
    locationSubTypeList: Array<any>;
    isClickEdit = false; // 是否处于编辑状态
    zoneIdDisable = true;

    constructor(
        private tools: ToolsService,
        private router: Router,
        // private incidentService: IncidentService,
        private fb: FormBuilder,
        private message: NzMessageService,
        private SEService: ScheduledService,
        private translate: TranslateService,
        private bully: BullySubjectService,
        private modalService: NzModalService,
        private pub: PublicService,
        private ss: SystemService,
        private authService: AuthService
    ) {}

    ngOnDestroy(): void {
        this.bully.resetBSubject();
        if (this.subscription) {
            this.bully.resetBSubject();
            this.subscription.unsubscribe();
        }
    }

    getSelectionList() {
        const { res1, res2, res3, res4 } = {
            res1: this.pub.getSelectOptionsList(SELECT_OPTIONS.EVENT_TYPE),
            res2: this.pub.getSelectOptionsList(SELECT_OPTIONS.EVENT_STATUS),
            res3: this.pub.getSelectOptionsList(SELECT_OPTIONS.LOCATION_TYPE),
            res4: this.pub.getSelectOptionsList(SELECT_OPTIONS.LOCATION_SUB_TYPE)
        };
        res1.data.forEach(item => {
            item.value = parseInt(item.value);
        });
        this.incidentType = res1.data;
        res2.data.forEach(item => {
            item.value = parseInt(item.value);
        });
        this.verificationStatus = res2.data.filter(item => +item.value === 0 || +item.value === 1);
        res3.data.forEach(item => {
            item.value = parseInt(item.value);
        });
        this.locationTypeList = res3.data;
        res4.data.forEach(item => {
            item.value = parseInt(item.value);
        });
        this.locationSubTypeList = res4.data;
    }

    initForm() {
        this.validateForm = this.fb.group({
            lane_Blockage: [{ value: null, disabled: this.isEditable }, Validators.required],
            eventId: [{ value: null, disabled: this.zoneIdDisable }],
            veri_State: [{ value: null, disabled: this.isEditable }, Validators.required],
            zone_ID: [{ value: null, disabled: this.zoneIdDisable }],
            type: [{ value: null, disabled: this.isEditable }, Validators.required],
            remark: [{ value: null, disabled: this.isEditable }],
            road_Name: [{ value: null, disabled: this.isEditable }, Validators.required],
            location_Type: [{ value: null, disabled: this.isEditable }, Validators.required],
            location_SubType: [{ value: null, disabled: this.isEditable }, Validators.required],
            start_Link_ID: [{ value: null, disabled: this.isEditable }],
            end_Link_ID: [{ value: null, disabled: this.isEditable }],
            start_X_Coor: [{ value: null, disabled: this.isEditable }, Validators.required],
            start_Y_Coor: [{ value: null, disabled: this.isEditable }, Validators.required],
            end_X_Coor: [{ value: null, disabled: this.isEditable }, Validators.required],
            end_Y_Coor: [{ value: null, disabled: this.isEditable }, Validators.required],
            planStartTime: [{ value: new Date(), disabled: this.isEditable }],
            planEndTime: [{ value: new Date(), disabled: this.isEditable }],
            causeIrId: [{ value: null, disabled: this.isEditable }],
            // causeIrId: [{ value: null, disabled: this.isEditable }],
            source: [{ value: null, disabled: this.isEditable }],
            cause: [{ value: null, disabled: this.isEditable }],
            direction: [{ value: null, disabled: this.isEditable }],
            startCoor: [{ value: null, disabled: this.isEditable }, Validators.required],
            endCoor: [{ value: null, disabled: this.isEditable }, Validators.required],
            q_End_Link_ID: [{ value: null, disabled: this.isEditable }, Validators.required],
            QendCoor: [{ value: null, disabled: this.isEditable }, Validators.required],
            // incidentTimeRange: [{ value: null, disabled: this.isEditable }, Validators.required],
            planStart: [{ value: null, disabled: this.isEditable }, Validators.required],
            QTimeRange: [{ value: null, disabled: this.isEditable }, Validators.required],
            // eTime: [{ value: null, disabled: this.isEditable }, Validators.required]
            planEnd: [{ value: null, disabled: this.isEditable }, Validators.required]
        });
        setTimeout(() => {
            for (const key in this.validateForm.controls) {
                if (this.validateForm.controls.hasOwnProperty(key)) {
                    this.validateForm.controls[key].enable();
                    // if (this.isUpdate) {
                    this.validateForm.controls['eventId'].disable();
                    this.validateForm.controls['zone_ID'].disable();
                    // }
                }
            }
        });
    }

    ngOnInit() {
        this.getSelectionList();
        this._setGisPosition();
        this.initForm();
        this.getZoneIdListByUser();
    }

    getZoneIdListByUser() {
        const queryData = {
            condition: {
                userId: localStorage.getItem('user_id'),
                user_Name: localStorage.getItem('user_name'),
                user_Pwd: null,
                organization_Id: null,
                userRole_Id: null,
                createTime: {
                    startTime: null,
                    endTime: null
                },
                updateTime: {
                    startTime: null,
                    endTime: null
                }
            },
            pageNum: 1,
            pageSize: 999999
        };
        this.SEService.getAllUser(queryData).subscribe(res => {
            const userZone = res.content.content[0].userZoneRel;
            userZone.forEach(zoneId => {
                this.zoneIdList.push(zoneId.vnqn_zone_id);
            });
        });
    }

    // 取消强制操作
    FourchHandleCancel() {
        this.isForceVisible = false;
    }

    // 确定强制操作
    ForceHandleOk() {
        this.isForceVisible = false;
        this.isForce = true;
        this.submitForm();
    }

    // 提交表单save按钮
    submitForm(): void {
        const value = this.queryDataForm(this.validateForm.value);
        const header = {
            zoneId: this.ss.userZoneList
                .filter(z => !z.isTemp)
                .map(zone => zone.id)
                .join(','),
            isFource: this.isForce
        };
        for (const i in this.validateForm.controls) {
            if (this.validateForm.controls.hasOwnProperty(i)) {
                this.validateForm.controls[i].markAsDirty();
                this.validateForm.controls[i].updateValueAndValidity();
            }
        }
        if (moment(value.end_Time).valueOf() < moment(value.start_Time).valueOf()) {
            this.message.create('success', 'Planned end time cannot be less than planned start time');
            return;
        }
        // this.SEService.
        // if (!value.road_Name) {
        //     this.message.create('error', `Please  fill in the required items!`);
        //     return;
        // }
        // if (!this.validateForm.valid) {
        //     this.message.create('error', `Please  fill in the required items!`);
        //     return;
        // }
        if (!value.lane_Blockage) {
            this.message.create('error', 'Lane Blockage cant be empty!');
            return;
        }
        if (value.planStart && value.road_Name && value.lane_Blockage) {
            if (this.isUpdate) {
                const queryData = {
                    end_Link_ID: value.end_Link_ID,
                    q_End_Link_ID: value.q_End_Link_ID,
                    end_X_Coor: value.end_X_Coor,
                    end_Y_Coor: value.end_Y_Coor,
                    eventId: this.validateForm.get('eventId').value,
                    lane_Blockage: value.lane_Blockage,
                    location_SubType: value.location_SubType,
                    location_Type: value.location_Type,
                    // planEndTime: value.end_Time,
                    planEndTime: value.planEnd,
                    planStartTime: value.planStart,
                    remark: value.remark,
                    road_Name: value.road_Name,
                    start_Link_ID: value.start_Link_ID,
                    start_X_Coor: value.start_X_Coor,
                    start_Y_Coor: value.start_Y_Coor,
                    type: value.type,
                    veri_State: value.veri_State,
                    zone_ID: this.validateForm.get('zone_ID').value,
                    Incident_Type: value.type,
                    q_Start_Time: value.QTimeRange[0],
                    q_End_Time: value.QTimeRange[1]
                };
                const query = this.SEService.updateScheduled(queryData, header);
                query.subscribe(
                    (res: any) => {
                        if (res['messagecode'] === '000000') {
                            this.isIrCreated.emit(queryData.eventId);
                            this.cancelEdit.emit('cancel');
                            this.isClickEdit = false;
                            this.clickEdit.emit(false);
                            this.translate.get('TrafficManagement.ScheduledEvent.update').subscribe(rest => {
                                this.message.create('success', rest);
                            });
                            // this.message.create('success', `TrafficManagement.ScheduledEvent.update`);
                        }
                        if (res['messagecode'] === '900103') {
                            this.isForceVisible = true;
                        }
                    },
                    (error: any) => {
                        // this.message.create('error', `TrafficManagement.ScheduledEvent.updateFail`);
                        this.translate.get('TrafficManagement.ScheduledEvent.updateFail').subscribe(rest => {
                            this.message.create('error', rest);
                        });
                    }
                );
            } else {
                // value.access = {
                //     ir_AccessType: false,
                //     userID: '2051',
                //     userType: null
                // };
                const queryData = {
                    end_Link_ID: value.end_Link_ID,
                    q_End_Link_ID: value.end_Link_ID,
                    end_X_Coor: value.end_X_Coor,
                    end_Y_Coor: value.end_Y_Coor,
                    eventId: this.validateForm.get('eventId').value,
                    lane_Blockage: value.lane_Blockage,
                    location_SubType: value.location_SubType,
                    location_Type: value.location_Type,
                    planEndTime: value.end_Time,
                    planStartTime: value.start_Time,
                    remark: value.remark,
                    road_Name: value.road_Name,
                    start_Link_ID: value.start_Link_ID,
                    start_X_Coor: value.start_X_Coor,
                    start_Y_Coor: value.start_Y_Coor,
                    type: value.type,
                    veri_State: value.veri_State,
                    zone_ID: this.validateForm.get('zone_ID').value,
                    q_Start_Time: value.QTimeRange[0],
                    q_End_Time: value.QTimeRange[1]
                };

                const query = this.SEService.createScheduled(queryData, header);
                query.subscribe(
                    (res: any) => {
                        if (res['messagecode'] === '000000') {
                            this.isIrCreated.emit(queryData.eventId);
                            this.cancelEdit.emit('cancel');
                            this.isClickEdit = false;
                            this.clickEdit.emit(false);
                            // 翻译
                            this.translate.get('TrafficManagement.ScheduledEvent.createSuccess').subscribe(rest => {
                                this.message.create('success', rest);
                            });
                            // this.message.create('success', `TrafficManagement.ScheduledEvent.createSuccess`);
                            // if (this.isTaskFromMap) {
                            //     this.incidentPosition.emit(res['data']);
                            //     this.message.create('success', `Create incident record success`);
                            // }
                        } else {
                            if (res['messagecode'] === '900103') {
                                this.isForceVisible = true;
                            }
                        }
                    },
                    (error: any) => {
                        // this.message.create('error', `Create incident record failed!`);
                        this.translate.get('TrafficManagement.ScheduledEvent.createFail').subscribe(rest => {
                            this.message.create('error', rest);
                        });
                    }
                );
            }
        } else {
            // this.message.create('error', `Please  fill in the required items!`);
            return;
        }
    }

    // 添加LaneBlockage
    addLaneBlockages(idx) {
        this.laneBlockagesList.push(idx);
    }

    // 删除LaneBlockage
    deletLaneBlockage(idx) {
        this.laneBlockagesList.splice(idx, 1);
    }

    initIrInfoDetail(item) {
        if (item != null) {
            /* if (!item.length) {
                item = null;
            }
            if (!item) {
                return;
            } */
            this.isUpdate = true;

            // 从其他页面调过来的创建任务
            // if (item.fromGis !== undefined) {
            //     this.isUpdate = false;
            //     if (item.fromGis) {
            //         this.isTaskFromMap = true;
            //     }
            // }

            // if (item.type === 2) {
            //     this.ifMoreInfoShow = true;
            // } else {
            //     this.ifMoreInfoShow = false;
            // }
            if (+item.veri_State === 1) {
                this.isBeginBow = true;
            } else {
                this.isBeginBow = false;
            }
            if (+item.veri_State === 0) {
                this.isVerify = true;
            } else {
                this.isVerify = false;
            }
            this.isEditable = true;
            if (item.laneBlockage != null) {
                this.laneBlockagesList = item.laneBlockage;
            }
            this.createAccess = item.access;
            this.validateForm.patchValue({
                lane_Blockage: item.laneBlockages,
                eventId: item.eventId,
                veri_State: item.veri_State,
                zone_ID: item.zone_ID || this.ss.userZoneList.filter(z => !z.isTemp)[0].id,
                type: item.type,
                remark: item.remark,
                road_Name: item.road_Name,
                location_Type: item.location_Type,
                location_SubType: item.location_SubType,
                start_Link_ID: item.start_Link_ID,
                end_Link_ID: item.end_Link_ID,
                start_X_Coor: item.start_X_Coor,
                start_Y_Coor: item.start_Y_Coor,
                end_X_Coor: item.end_X_Coor,
                end_Y_Coor: item.start_Y_Coor,
                planStartTime: item.planStartTime,
                planEndTime: item.planEndTime,
                startCoor: item.start_X_Coor + ' , ' + item.start_Y_Coor,
                endCoor: item.end_X_Coor && item.end_Y_Coor ? item.end_X_Coor + ' , ' + item.end_Y_Coor : null,
                // // QendCoor: item.start_Link_ID + ' , ' + item.end_Link_ID,
                // // incidentTimeRange: item.planStartTime ? [new Date(item.planStartTime), new Date(item.planEndTime)] : null,
                planStart: item.planStartTime ? new Date(item.planStartTime) : null,
                causeIrId: item.cause,
                source: item.source,
                cause: item.cause,
                direction: item.direction,
                // startCoor: item.startCoor,
                // endCoor: item.endCoor,
                q_End_Link_ID: item.end_Link_ID,
                // q_End_Link_ID: item.start_Link_ID + ' ,' + item.end_Link_ID,
                QendCoor: item.end_X_Coor && item.end_Y_Coor ? item.end_X_Coor + ' , ' + item.end_Y_Coor : null,
                // incidentTimeRange: item.incidentTimeRange,
                QTimeRange: item.planStartTime && item.planEndTime ? [new Date(item.planStartTime), new Date(item.planEndTime)] : null,
                // eTime: item.eTime,
                planEnd: item.planEndTime ? new Date(item.planEndTime) : null
            });
            for (const key in this.validateForm.controls) {
                if (this.validateForm.controls.hasOwnProperty(key)) {
                    this.validateForm.controls[key].disable();
                }
            }
            // this.editForm();
            if (!item.eventId) {
                this.isUpdate = false;
            }
        } else {
            this.isEditable = false;
            this.validateForm.reset();
            this.isUpdate = false;
            for (const key in this.validateForm.controls) {
                if (this.validateForm.controls.hasOwnProperty(key)) {
                    this.validateForm.controls[key].enable();
                    this.validateForm.controls['eventId'].disable();
                    this.validateForm.controls['zone_ID'].disable();
                }
            }
        }
    }

    back() {
        this.isClickEdit = false;
        this.cancelEdit.emit('cancel');
    }

    // 修改表单
    editForm() {
        if (!this.isUserZoneChecked()) {
            return;
        }
        this.isClickEdit = true;
        this.clickEdit.emit(true);
        for (const key in this.validateForm.controls) {
            if (this.validateForm.controls.hasOwnProperty(key)) {
                this.validateForm.controls[key].enable();
                this.validateForm.controls['eventId'].disable();
                this.validateForm.controls['zone_ID'].disable();
            }
        }
    }

    // 验证
    verify() {
        if (!this.isUserZoneChecked()) {
            return;
        }
        const header = {
            zoneId: this.zoneIdList.join(','),
            isFource: this.isForce
        };
        const tempTranslate = [{ title: 'TrafficManagement.IncidentRecord.Please verify this scheduled event' }];
        tempTranslate.forEach(item => {
            this.translate.get(item.title).subscribe((res: string) => {
                item.title = res || '';
            });
        });
        this.modalService.confirm({
            nzTitle: '<i>' + tempTranslate[0].title + '</i>',
            nzContent: '',
            nzOnOk: () => {
                const value = this.queryDataForm(this.validateForm.value);

                const queryData = {
                    end_Link_ID: value.end_Link_ID,
                    end_X_Coor: value.end_X_Coor,
                    end_Y_Coor: value.end_Y_Coor,
                    eventId: this.validateForm.get('eventId').value,
                    lane_Blockage: value.lane_Blockage,
                    location_SubType: value.location_SubType,
                    location_Type: value.location_Type,
                    planEndTime: value.planEnd,
                    planStartTime: value.start_Time,
                    remark: value.remark,
                    road_Name: value.road_Name,
                    start_Link_ID: value.start_Link_ID,
                    start_X_Coor: value.start_X_Coor,
                    start_Y_Coor: value.start_Y_Coor,
                    type: value.type,
                    veri_State: value.veri_State,
                    zone_ID: this.validateForm.get('zone_ID').value
                    // QTimeRange:value.QTimeRange
                };
                // 将状态更改为Notified
                const query = this.SEService.updateScheduled(queryData, header);
                query.subscribe(
                    (ele: any) => {
                        if (ele['messagecode'] === '000000') {
                            this.isIrCreated.emit(queryData.eventId);
                            this.cancelEdit.emit('cancel');
                        }
                    },
                    (error: any) => {
                        // this.message.create('error', `TrafficManagement.ScheduledEvent.updateFail`);
                        this.translate.get('TrafficManagement.ScheduledEvent.updateFail').subscribe(rest => {
                            this.message.create('error', rest);
                        });
                    }
                );
            }
        });
    }

    // 将计划事件设为待启动状态
    beginNow(param?) {
        const header = {
            zoneId: this.zoneIdList.join(','),
            isFource: param ? param : false
        };
        const date = new Date().getTime();
        const value = this.queryDataForm(this.validateForm.value);
        // this.cancelEdit.emit('cancel');
        // 当到达计划启动时间时启动
        // if (value === date) {
        this.SEService.readySatartScheduled(value.eventId, header).subscribe(res => {
            if (res.messagecode === '900103') {
                this.isForceBeginNowVisible = true;
                return;
            }
            if (res.messagecode === '000000') {
                this.cancelEdit.emit('cancel');

                setTimeout(() => {
                    // 直接跳转到traffic-management/incident-record页面
                    this.router.navigate(['ccm/traffic-management/incident-record']);
                    const queryData = {
                        end_Link_ID: value.end_Link_ID,
                        end_X_Coor: value.end_X_Coor,
                        end_Y_Coor: value.end_Y_Coor,
                        eventId: this.validateForm.get('eventId').value,
                        lane_Blockage: value.lane_Blockage,
                        location_SubType: value.location_SubType,
                        location_Type: value.location_Type,
                        planEndTime: value.end_Time,
                        planStartTime: value.start_Time,
                        remark: value.remark,
                        road_Name: value.road_Name,
                        start_Link_ID: value.start_Link_ID,
                        start_X_Coor: value.start_X_Coor,
                        start_Y_Coor: value.start_Y_Coor,
                        type: value.type,
                        veri_State: '4',
                        zone_ID: this.validateForm.get('zone_ID').value
                    };
                    // 将状态更改为Executed
                    const query = this.SEService.updateScheduled(queryData, header);
                    query.subscribe(
                        (ele: any) => {
                            if (ele['messagecode'] === '000000') {
                                this.isIrCreated.emit(queryData.eventId);
                            }
                        },
                        (error: any) => {
                            // this.message.create('error', `TrafficManagement.ScheduledEvent.updateFail`);
                            this.translate.get('TrafficManagement.ScheduledEvent.updateFail').subscribe(rest => {
                                this.message.create('error', rest);
                            });
                        }
                    );
                }, 2000);
            }
        });
        // }
    }

    // 取消计划事件
    cancelEditFunc(param?) {
        if (!this.isUserZoneChecked()) {
            return;
        }
        const header = {
            zoneId: this.ss.userZoneList
                .filter(z => !z.isTemp)
                .map(zone => zone.id)
                .join(','),
            isFource: param ? param : this.isCancelForce
        };
        // this.cancelEdit.emit('cancel');
        const value = this.queryDataForm(this.validateForm.value);
        this.SEService.cancelScheduled(value.eventId, header).subscribe(res => {
            if (res.messagecode === '000000') {
                // this.message.create('success', `TrafficManagement.ScheduledEvent.cancelSuccess`);
                this.translate.get('TrafficManagement.ScheduledEvent.cancelSuccess').subscribe(rest => {
                    this.message.create('success', rest);
                });
                const queryData = {
                    end_Link_ID: value.end_Link_ID,
                    end_X_Coor: value.end_X_Coor,
                    end_Y_Coor: value.end_Y_Coor,
                    eventId: this.validateForm.get('eventId').value,
                    lane_Blockage: value.lane_Blockage,
                    location_SubType: value.location_SubType,
                    location_Type: value.location_Type,
                    planEndTime: value.end_Time,
                    planStartTime: value.start_Time,
                    remark: value.remark,
                    road_Name: value.road_Name,
                    start_Link_ID: value.start_Link_ID,
                    start_X_Coor: value.start_X_Coor,
                    start_Y_Coor: value.start_Y_Coor,
                    type: value.type,
                    veri_State: '3',
                    zone_ID: this.validateForm.get('zone_ID').value
                };
                // 将状态更改为Cancelled
                const query = this.SEService.updateScheduled(queryData, header);
                query.subscribe(
                    (ele: any) => {
                        if (ele['messagecode'] === '000000') {
                            this.isIrCreated.emit(queryData.eventId);
                            this.cancelEdit.emit('cancel');
                            // this.cancelEdit.emit('cancel');
                            // this.message.create('success', `Update incident record success!`);
                        }
                    },
                    (error: any) => {
                        // this.message.create('error', `TrafficManagement.ScheduledEvent.updateFail`);
                        this.translate.get('TrafficManagement.ScheduledEvent.updateFail').subscribe(rest => {
                            this.message.create('error', rest);
                        });
                    }
                );
                this.cancelEdit.emit('cancel');
            } else if (res.messagecode === '900103') {
                this.isForceCancelVisible = true;
            }
        });
    }

    // const today = new Date();

    // const timeDefaultValue = new Date();

    // 禁止incidentTime选择今日之前的日期
    range(start: number, end: number): number[] {
        const result = [];
        for (let i = start; i < end; i++) {
            result.push(i);
        }
        return result;
    }

    disabledDate = (current: Date): boolean => {
        // Can not select days before today and today
        let kk;
        if (differenceInCalendarDays(current, new Date()) < 0) {
            kk = true;
        } else {
            kk = false;
        }
        return kk;
    };
    disabledDateTime = (): object => {
        return {
            nzDisabledHours: () => this.range(0, 24).splice(4, 20),
            nzDisabledMinutes: () => this.range(30, 60),
            nzDisabledSeconds: () => [55, 56]
        };
    };
    disabledRangeTime = (value: Date[], type: 'start' | 'end'): object => {
        if (type === 'start') {
            return {
                nzDisabledHours: () => this.range(0, 60).splice(4, 20),
                nzDisabledMinutes: () => this.range(30, 60),
                nzDisabledSeconds: () => [55, 56]
            };
        }
        return {
            nzDisabledHours: () => this.range(0, 60).splice(20, 4),
            nzDisabledMinutes: () => this.range(0, 31),
            nzDisabledSeconds: () => [55, 56]
        };
    };

    // // finish
    // finish() { }
    // // 展示更多信息
    // showModal() { }

    // split
    queryDataForm(item) {
        const value = this.tools.isClone(item);
        if (value.startCoor !== null) {
            const startPoint = value.startCoor.split(',');
            value.start_X_Coor = startPoint[0];
            value.start_Y_Coor = startPoint[1];
        }
        if (value.endCoor !== null) {
            const endPoint = value.endCoor.split(',');
            value.end_X_Coor = endPoint[0];
            value.end_Y_Coor = endPoint[1];
        }
        // if (value.QendCoor !== null) {
        //     const QendPoint = value.QendCoor.split(',');
        //     value.q_End_X_Coor = QendPoint[0];
        //     value.q_End_Y_Coor = QendPoint[1];
        // }
        if (value.q_End_Link_ID !== null) {
            // const QendPoint = value.q_End_Link_ID.split(',');
            // value.start_Link_ID = QendPoint[0];
            // value.end_Link_ID = QendPoint[1];
            const QendPoint = value.q_End_Link_ID;
            value.end_Link_ID = QendPoint;
        }
        if (this.laneBlockagesList !== null) {
            value.lane_Blockage = this.laneBlockagesList.join('');
        }
        value.start_Time = new Date(value.planStart).getTime();
        value.end_Time = new Date(value.planEnd).getTime();
        // if (value.incidentTimeRange !== null && value.incidentTimeRange.length !== 0) {
        //     value.start_Time = new Date(value.incidentTimeRange[0]).getTime();
        //     value.end_Time = new Date(value.incidentTimeRange[1]).getTime();
        // }
        // if (value.QTimeRange !== null && value.QTimeRange.length !== 0) {
        //     value.q_Start_Time = new Date(value.QTimeRange[0]).getTime();
        //     value.q_End_Time = new Date(value.QTimeRange[1]).getTime();
        // }
        return value;
    }

    getGisPosition(key) {
        // const url = `${location.origin}/#/gis`;
        // window.open(url, 'getPosition');
        if (!this.isClickEdit) {
            return;
        }
        const bully = {
            type: MESSAGE_CHANNEL.CCM_CHANNEL,
            data: {
                data: key
            }
        };
        this.bully.registerBroadcastSend(MESSAGE_CHANNEL.CCM_CHANNEL, bully);
        this.inGet[key] = true;
    }

    private _setGisPosition() {
        this.subscription = this.bully.registerBroadcastReceive(MESSAGE_CHANNEL.MAP_CHANNEL).subscribe(res => {
            if (res.type === MESSAGE_CHANNEL.MAP_CHANNEL) {
                const features = res['data']['features'];
                const dataType = res['data']['dataType'];
                const value = {
                    [dataType]: features['longitude'] + ',' + features['latitude']
                };
                switch (dataType) {
                    case 'startCoor':
                        value['road_Name'] = features['roadName'];
                        break;
                    case 'endCoor':
                        value['q_End_Link_ID'] = features['linkID'];
                        break;
                }
                this.inGet[dataType] = false;
                this.validateForm.patchValue(value);
            }
        });
    }

    /**
     * 是否有用户zone权限
     * @returns
     */
    isUserZoneChecked() {
        if (!this.isUpdate) {
            return true;
        }
        return this.authService.isUserZoneChecked(this.validateForm.get('zone_ID').value);
    }
}
